/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.resources.impl.internal;

import java.io.File;
import java.util.Map;

import nemos.resources.impl.IExternalFolder;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Service;

/**
 * ExternalFolderImpl : The {@link IExternalFolder} simple implementation.
 * 
 * <p>
 * Services can be instanciated through the ConfigAdmin mechanism. It simply
 * locate files in the specified folder and build the file name with the
 * specified prefix and suffix.
 * </p>
 * 
 * @author Romain DUBOIS
 */
@Component(specVersion = "1.1", name = "nemos.resources.folder", policy = ConfigurationPolicy.REQUIRE)
@Service(IExternalFolder.class)
public class ExternalFolderImpl extends AbstractFileManager implements IExternalFolder {

    /** The folder path property */
    private static final String PATH_PROPERTY = "nemos.resources.folder.path"; //$NON-NLS-1$
    /** The files prefix property */
    private static final String PREFIX_PROPERTY = "nemos.resources.folder.prefix"; //$NON-NLS-1$
    /** The files suffix property */
    private static final String SUFFIX_PROPERTY = "nemos.resources.folder.suffix"; //$NON-NLS-1$

    /** The folder */
    private File mFolder;
    /** The files prefix */
    private String mPrefix;
    /** The files suffix */
    private String mSuffix;

    /**
     * Constructor
     */
    public ExternalFolderImpl() {
        super();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.resources.impl.IExternalFolder#getExternalFile(java.lang.String)
     */
    @Override
    public File getExternalFile(final String pName) {
        return new File(mFolder, mPrefix + pName + mSuffix);
    }

    /**
     * Start the component.
     * 
     * @param pProperties
     *            The component properties
     */
    @Activate
    public void start(final Map<String, Object> pProperties) {
        updated(pProperties);
    }

    /**
     * Update the component properties.
     * 
     * @param pProperties
     *            The component properties
     */
    @Modified
    public void updated(final Map<String, Object> pProperties) {
        mPrefix = escape(getProperty(pProperties, PREFIX_PROPERTY, "")); //$NON-NLS-1$
        mSuffix = escape(getProperty(pProperties, SUFFIX_PROPERTY, "")); //$NON-NLS-1$

        mFolder = checkOrCreateFolder(pProperties, PATH_PROPERTY, null);
    }

    /**
     * Get a String property for the properties map.
     * 
     * @param pProperties
     *            The properties map
     * @param pName
     *            The propety name
     * @param pDefault
     *            The default value
     * @return The property value
     */
    private String getProperty(final Map<String, Object> pProperties, final String pName,
            final String pDefault) {
        final Object lValue = pProperties.get(pName);

        if (lValue instanceof String) {
            return (String) lValue;
        }

        return pDefault;
    }
}
